﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace program1
{
    public class ClassService
    {
        private readonly ClassRepository classRepository;
        private readonly LogRepository logRepository;

        public ClassService(string connectionString)
        {
            classRepository = new ClassRepository(connectionString);
            logRepository = new LogRepository(connectionString);
        }

        public void AddClass(int schoolId, string name, string teacher)
        {
            classRepository.InsertClass(schoolId, name, teacher);
            logRepository.LogAction("Add Class", "Classes", $"Added a new class with name: {name}");
        }

        public void DeleteClass(int classId)
        {
            var classToDelete = classRepository.GetClassById(classId);
            if (classToDelete != null)
            {
                classRepository.DeleteClass(classId);
                logRepository.LogAction("Delete Class", "Classes", $"Deleted class with ID: {classId}, Name: {classToDelete.Name}");
            }
        }

        public void UpdateClass(int classId, string name, string teacher)
        {
            var oldClass = classRepository.GetClassById(classId);
            if (oldClass != null)
            {
                classRepository.UpdateClass(classId, name, teacher);
                logRepository.LogAction("Update Class", "Classes", $"Updated class with ID: {classId}, Name: {oldClass.Name} -> {name}");
            }
        }

        public List<Class> GetAllClasses()
        {
            return classRepository.GetAllClasses();
        }

    }

}
